package com.zdx.yswl.ui.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.umeng.analytics.MobclickAgent;
import com.zdx.yswl.R;
import com.zdx.yswl.http.KAsyncTask.TaskDelegate;
import com.zdx.yswl.ui.adapter.PanoramaAdapter;
import com.zdx.yswl.ui.base.BaseActivity;
import com.zdx.yswl.ui.base.BaseWebViewActivity;
import com.zdx.yswl.ui.bean.PanoramaBean;
import com.zdx.yswl.ui.exception.AppException;
import com.zdx.yswl.ui.exception.NetworkException;
import com.zdx.yswl.ui.exception.NoDataException;
import com.zdx.yswl.ui.task.PanoramaTask;
import com.zdx.yswl.view.dialog.ConfirmDialog;
import com.zdx.yswl.view.dialog.ConfirmDialog.ConfirmDialogDelegate;

public class PanoramaActivity extends BaseActivity implements TaskDelegate {

	protected static final String TAG = PanoramaActivity.class.getSimpleName();

	private String requestId;

	public static Intent actionStart(Context context, String requestId) {
		Intent intent = new Intent(context, PanoramaActivity.class);
		intent.putExtra("projectId", requestId);
		return intent;
	}

	PanoramaAdapter mAdapter;

	@Override
	protected void initSettings()
	{

	}

	@Override
	protected void initUi()
	{

	}

	@Override
	protected void saveSettings()
	{

	}

	@Override
	protected void freeMe()
	{

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_panorama);
		requestId = getIntent().getStringExtra("projectId");
		initNav();
		initData();
	}

	private void initNav() {
		setHeader("3D全景");
	}

	private void initData() {
		ListView list = (ListView) findViewById(R.id.list_view);
		mAdapter = new PanoramaAdapter(this);
		list.setAdapter(mAdapter);

		PanoramaTask task = new PanoramaTask(-1, this);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("projectId", requestId);
		task.execute(map);

		list.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				PanoramaBean data = mAdapter.dataList.get(position);
				Intent intent = BaseWebViewActivity.createIntent(
						PanoramaActivity.this, data.url, data.name, false);
				startActivity(intent);
			}
		});
	}

	/** 友盟session的统计 */
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	/** 友盟session的统计 */
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

	@Override
	public void faild_TaskDelegate(int taskId, Object object) {
		if (object instanceof NoDataException) {
			Toast.makeText(this, ((NoDataException) object).getMessage(), Toast.LENGTH_SHORT).show();
		} else if (object instanceof NetworkException) {
			Toast.makeText(this, "网络异常", Toast.LENGTH_SHORT).show();
		} else if (object instanceof AppException) {
			ConfirmDialog.share(this, -1, new ConfirmDialogDelegate() {
				@Override
				public void onClick_ConfirmDialog(int __id, int __position) {
					finish();
				}
			});
			ConfirmDialog.showDialog("提示", ((AppException) object).getMessage(), "确定");
		}
	}

	@Override
	public void success_TaskDelegate(int taskId, Object object) {
		ArrayList<PanoramaBean> list = (ArrayList<PanoramaBean>) object;
		mAdapter.dataList = list;
		mAdapter.notifyDataSetChanged();

	}

	@Override
	public void onNotify(int what, int arg1, int arg2, Object obj)
	{

	}

	@Override
	public void onHandler(int what, int arg1, int arg2, Object obj)
	{

	}
}
